package com.Util;


import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.UUID;

/**
 * 文件上传
 */
@Slf4j
public class FileUntil {

    /**
     * File 文件的上传处理(OSS)
     * @param file
     * @return
     */
    public static String upload( File file,Boolean isRename) {
        // 获取文件名
        String fileName = file.getName ();
        // 获取文件的后缀名
        if (isRename) {
            String suffixName = fileName.substring ( fileName.lastIndexOf ( "." ) );
            fileName = UtilBase.getCurrentTime () + suffixName;
        }
        try {
            String endpoint = Config.ENDPOINT;
            String accessKeyId = Config.ACCESSKEYID;
            String accessKeySecret = Config.ACCESSKEYSECRET;
            // 创建OSSClient实例
            OSSClient client = new OSSClient (endpoint, accessKeyId, accessKeySecret);
            //初始化储存的路径
            String url="image/"+fileName;
            client.putObject("hdjun", url, file );
            return url.replaceFirst ( "jersey","" );
        } catch (IllegalStateException e) {
            log.error ( "【文件错误】={}",e );
        } catch (Exception e) {
            log.error ( "【文件错误】={}",e );
        }
        return null;
    }

    public static void main(String[] args) {
        File file=new File ( "E:\\gitHtml\\HDJ_html\\images\\1.jpg" );
        upload ( file,true );
        System.out.println ("上传完毕");

    }


}